<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>竞数登录界面</title>
	<link rel="stylesheet" href="assets/login.css">
</head>

<body>
	<div class="loginTop">
		<div class="loginIcon">
			<img src="http://first.52carlife.com/jingshulogo@2x.png" alt="52">
		</div>
		<div class="loginContent" id="loginContent">
			<div>账号登录</div>
			<div>
				<div class="underline"></div>
			</div>
			<div class="inputBox">
				<i class="icon-user"></i>
				<input type="text" placeholder="用户名" id="nameIn">
			</div>
			<div class="inputBox">
				<i class="icon-pwd"></i>
				<input type="password" placeholder="密码" id="pwdIn">
			</div>
			<!-- 验证码 -->
			<div class="identCode">
				<input type="text" placeholder="请输入验证码" id="codeImage">
				<img id="codeImg">
			</div>
			<!-- 记住我 -->
			<div class="rememberMe">
				<div class="hook" id="btnHook">
					<img id="hookIcon" src="http://first.52carlife.com/jingshu%E2%88%9A@2x.png" alt="52">
				</div>
				<div>记住我</div>
			</div>
			<!-- 登录 -->
			<div class="loginBtn" id="loginBtn">
				<button>登录</button>
			</div>
		</div>
		<!-- 如果登录成功就影藏loginIcon -->
		<div class="itemNccc" style="display: none" id="itemNccc">
			<select name="itemSelect" id="itemSelect">
			</select>
		</div>
	</div>
	<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
	<script>
		// var select = document.getElementById("itemSelect");
		// alert(rtl.options.(rtl.selectedIndex).value);
		//1.如何获得当前选中的值？：
		// var values = select.value;
		// console.log(values+"SSSS");
		// 成功获取bossid
		var bossId;
		var codeToken;
		$(function () {
			// 直接获取（验证码图片）
			$.ajax({
				type: "POST",
				url: "/loginValidCode",
				headers: {
					'content-type': 'application/x-www-form-urlencoded'
				},
				success: function (data) {
					console.log(data);
					var src = $('#codeImg').attr('src');
					// 获取赋值
					var s = data.data.codeBase64;
					codeToken = data.data.codeToken;
					console.log(codeToken);
					$('#codeImg').attr('src', s);

				},
				error: function (jqXHR) {
					alert("发生错误：" + jqXHR.status);
				},
				complete: function () {
					console.log("成功不成")
				}
			})
			$("#btnHook").click(function () {
				$("#hookIcon").toggle();
			});

		});

		// 获取输入框的值
		$('#loginBtn').click(function () {
			// 获取了用户名
			var nameIn = $('#nameIn').val();
			// 获取密码
			var pwdIn = $('#pwdIn').val();
			// 获取验证码
			var code = $('#codeImage').val();
			console.log(codeToken)
			var datea = {
				"code": code,
				"codeToken": codeToken,
				"username": "17720517619",
				"password": "123456",
			};
			var d = JSON.stringify(datea);
			console.log(d);
			$.ajax({
				type: "POST",
				url: "/login",
				data: d,
				headers: {
					'content-type': 'application/json',
				},
				success: function (data) {
					console.log(data);
					// 如果成功了就隐藏登录页显示下拉列表
					bossId = data.data.id;
					console.log(bossId);
					// 存bossid
					localStorage.setItem("bossid",bossId);
					itemSelect();
				},
				error: function (jqXHR) {
					alert("发生错误：" + jqXHR.status);
				},
				complete: function () {
					console.log("成功不成")
				}
			})
		})

		// 获取分机构id
		function itemSelect() {
			$.ajax({
				type: "POST",
				url: "/bossOrganizationList",
				data: {
					"bossId": bossId
				},
				success: function (data) {
					console.log(data);
					$("#itemNccc").css("display","inline");
					$("#loginContent").css("display","none");
					// 获取机构的名字赋值到下拉列表
					let selectName = data.data;
					console.log(selectName);
					for (let i = 0; i < selectName.length; i++) {
						console.log(selectName[i].name);
						$("#itemSelect").append(`<option value='${selectName[i].id}'>${selectName[i].name}</option>`)
					}
				},
				error: function (jqXHR) {
					alert("发生错误：" + jqXHR.status);
				},
				complete: function () {
					console.log("成功不成")
				}
			})
		}
		// 获取机构id
		var itemCode;
		  // 本地存储比传值过去好看
		//   localStorage.setItem("bossid",bossId);
		//   localStorage.setItem("itemCode",itemCode);
		
		$("#itemSelect").change(function () {
			// if ($(this).children('option:selected')[0].childNodes[0]) {
			// 	if ($(this).children('option:selected')[0].childNodes[0].data) {
			// 		var p1 = $(this).children('option:selected')[0].childNodes[0].data; //这就是selected的值 
			// 		//做出相应操作
			// 		console.log(p1);
			// 	}
			// }
			// var itemCode = $("#itemSelect").val();
			// console.log($(itemCode+"点击的机构名称")
			 itemCode = $("#itemSelect").val();
			//  存机构id
			 localStorage.setItem("itemCode",itemCode);
			//  已经获取
			//  var bossId1 = localStorage.getItem("bossid");
			//  console.log("ssss"+bossId1);
			console.log(itemCode+"点击获取的机构id")
			window.location.href="pages/setTime.html";
		})
		
		 
	</script>
</body>

</html>